/**
 * 
 */
package edu.rice.test.onedimension;

import java.util.ArrayList;

/**
 * @author frogy
 * 
 */
public class Agent {
	private double x;

	private double v;

	private double dir;

	private double L;

	private double ph;

	private double w;

	private int gridX;

	private double xo;

	private double diro;

	private double pho;

	private Agent[] list;

	public Agent(double X, Agent[] List) {
		x = X * Math.random();
		v = 0.05;
		w = 0.01;
		dir = Math.random() > 0.5 ? 0 : Math.PI;
		L = 7.0;
		ph = Math.random() * 2 * Math.PI;
		gridX = (int) x;
		xo = x;
		diro = dir;
		pho = ph;
		list = List;
	}

	public void step(double X) {
		signal();
		ph += w * 5;
		if (ph > 2 * Math.PI) {
			ph = 0;
			dir += Math.PI;
			if (dir >= 2 * Math.PI) {
				dir -= 2 * Math.PI;
			}
			if (dir <= -Math.PI) {
				dir += 2 * Math.PI;
			}
		}
		x += v * Math.cos(dir) * 5;
		x = (x + X) % X;
	}

	public void updateOldValue() {
		xo = x;
		diro = dir;
		pho = ph;
		gridX = (int)x;
	}

	public void signal() {
		for (int i = 0; i < list.length; i++) {
			if (dir != list[i].getDiro()) {
				if (dist(list[i]) < L / 5) {
					if (ph > 2) {
						ph += 2 * Math.PI;
					}
				}
			}
		}
	}

	private double dist(Agent a) {
		double dis = Math.abs(x - a.getXo());
		return dis;
	}

	/**
	 * @return the x
	 */
	public double getX() {
		return x;
	}

	/**
	 * @param x
	 *            the x to set
	 */
	public void setX(double x) {
		this.x = x;
	}

	/**
	 * @return the v
	 */
	public double getV() {
		return v;
	}

	/**
	 * @param v
	 *            the v to set
	 */
	public void setV(double v) {
		this.v = v;
	}

	/**
	 * @return the dir
	 */
	public double getDir() {
		return dir;
	}

	/**
	 * @param dir
	 *            the dir to set
	 */
	public void setDir(double dir) {
		this.dir = dir;
	}

	/**
	 * @return the l
	 */
	public double getL() {
		return L;
	}

	/**
	 * @param l
	 *            the l to set
	 */
	public void setL(double l) {
		L = l;
	}

	/**
	 * @return the ph
	 */
	public double getPh() {
		return ph;
	}

	/**
	 * @param ph
	 *            the ph to set
	 */
	public void setPh(double ph) {
		this.ph = ph;
	}

	/**
	 * @return the gridX
	 */
	public int getGridX() {
		return gridX;
	}

	/**
	 * @param gridX
	 *            the gridX to set
	 */
	public void setGridX(int gridX) {
		this.gridX = gridX;
	}

	/**
	 * @return the xo
	 */
	public double getXo() {
		return xo;
	}

	/**
	 * @param xo
	 *            the xo to set
	 */
	public void setXo(double xo) {
		this.xo = xo;
	}

	/**
	 * @return the diro
	 */
	public double getDiro() {
		return diro;
	}

	/**
	 * @param diro
	 *            the diro to set
	 */
	public void setDiro(double diro) {
		this.diro = diro;
	}

	/**
	 * @return the pho
	 */
	public double getPho() {
		return pho;
	}

	/**
	 * @param pho
	 *            the pho to set
	 */
	public void setPho(double pho) {
		this.pho = pho;
	}

	/**
	 * @return the list
	 */
	public Agent[] getList() {
		return list;
	}

	/**
	 * @param list
	 *            the list to set
	 */
	public void setList(Agent[] list) {
		this.list = list;
	}

}
